package org.jeecg.system.project.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.util.DataUtil;
import org.jeecg.system.project.entity.ProjectTeam;
import org.jeecg.system.project.mapper.ProjectTeamMapper;
import org.jeecg.system.project.service.IProjectTeamService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.unit.DataUnit;

import java.util.List;

/**
 * @Description: 分队负责项目表
 * @Author: jeecg-boot
 * @Date:   2020-06-11
 * @Version: V1.0
 */
@Service
public class ProjectTeamServiceImpl extends ServiceImpl<ProjectTeamMapper, ProjectTeam> implements IProjectTeamService {


    /**
     * 保存负责推进项目的小分队
     * @param lists
     * @param projectId
     * @return
     */
    @Override
    public boolean saveOrUpdateTeam(List<ProjectTeam> lists,String projectId,double totalAmount ) {

        if (CollectionUtils.isEmpty(lists)) {
            return false;
        }
        for (ProjectTeam item : lists) {
            item.setProjectId(projectId);
            ProjectTeam queryteam   = new ProjectTeam();
            queryteam.setProjectId(projectId);
            queryteam.setUsername(item.getUsername());
            QueryWrapper<ProjectTeam> queryWrapper = QueryGenerator.initQueryWrapper(queryteam,null);
            ProjectTeam one = this.getOne(queryWrapper);
            item.setFlags(1);
            double realNum  = (1.0/lists.size()); //如果共同推进，同个项目均分
            realNum = DataUtil.keepNums(realNum,2);

            double amount  = totalAmount/lists.size();
            amount =DataUtil.keepNums(amount,3);

            System.out.println(realNum);

            if (ObjectUtil.isEmpty(one)) {
                item.setRealNum(realNum);
                item.setTotalAmount(item.getTotalAmount()==0?amount:item.getTotalAmount());
                item.setDateTime(DateUtils.date2Str(DateUtils.yyyyMM.get()));//默认当前月
                this.save(item);
            }else {
                one.setRealNum(realNum);
                item.setTotalAmount(item.getTotalAmount()==0?amount:item.getTotalAmount());
                this.updateById(one);
            }
        }

        return true;
    }
}
